package com.hfad.todolistapp;

import android.content.DialogInterface;
import android.graphics.drawable.Drawable;
import android.os.Bundle;

import com.hfad.todolistapp.service.StatusService;
import com.hfad.todolistapp.model.TodoItem;
import com.google.gson.Gson;

import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

/**
 * A fragment representing a single Item detail screen.
 * This fragment is either contained in a {@link ItemListActivity}
 * in two-pane mode (on tablets) or a {@link ItemDetailActivity}
 * on handsets.
 */
public class ItemDetailFragment extends Fragment {
    public static final String ARG_ITEM = "item";
    public static final String ARG_INDEX = "index";

    private ImageView mImageStatus;
    private TextView mTextStatus;

    private TodoItem mItem;
    private int mItemIndex = -1;
    private String[] mStatusOptions;

    public ItemDetailFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initStatusOptions();

        if (getArguments().containsKey(ARG_ITEM)) {
            String modelStr = getArguments().getString(ARG_ITEM);
            mItem = new Gson().fromJson(modelStr, TodoItem.class);
            mItemIndex = getArguments().getInt(ARG_INDEX);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.item_detail, container, false);

        if (mItem != null) {
            ((TextView) rootView.findViewById(R.id.item_title)).setText(mItem.getTitle());
            Drawable statusBackgroundRes = StatusService.getStatusDrawable(
                    rootView.getContext(), mItem.getStatus()
            );
            mImageStatus = rootView.findViewById(R.id.image_status);
            mImageStatus.setImageDrawable(statusBackgroundRes);

            String statusText = StatusService.getStatusText(getActivity(), mItem.getStatus());
            mTextStatus = rootView.findViewById(R.id.text_status);
            mTextStatus.setText(statusText);

            ((TextView) rootView.findViewById(R.id.item_description)).setText(mItem.getDescription());


            ((Button) rootView.findViewById(R.id.btn_change_status)).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    new AlertDialog.Builder(getActivity())
                            .setTitle("Change Status")
                            .setSingleChoiceItems(
                                    mStatusOptions,
                                    getStatusMenuIndex(mItem),
                                    new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int statusIndex) {
                                            TodoItem.ItemStatus status = TodoItem.ItemStatus.TODO;
                                            if (statusIndex == 1) {
                                                status = TodoItem.ItemStatus.IN_PROGRESS;
                                            } else if (statusIndex == 2) {
                                                status = TodoItem.ItemStatus.COMPLETE;
                                            }
                                            ((ToDoStatusChangeCallback)getActivity()).onItemStatusChange(mItem.getId(), status);
                                            mItem.setStatus(status);
                                            updateStatusView(status);
                                            dialog.dismiss();
                                            Toast.makeText(getActivity(), "Update Status", Toast.LENGTH_SHORT).show();
                                        }
                                    }
                            )
                            .show();
                }
            });
        }

        return rootView;
    }

    private void updateStatusView(TodoItem.ItemStatus status) {
        Drawable icon = StatusService.getStatusDrawable(getActivity(), status);
        String text = StatusService.getStatusText(getActivity(), status);
        mImageStatus.setImageDrawable(icon);
        mTextStatus.setText(text);
    }

    private int getStatusMenuIndex(TodoItem item) {
        if (item.getStatus() == TodoItem.ItemStatus.IN_PROGRESS) {
            return 1;
        } else if (item.getStatus() == TodoItem.ItemStatus.COMPLETE) {
            return 2;
        } else {
            return 0;
        }
    }

    private void initStatusOptions() {
        mStatusOptions = new String[]{
                getActivity().getResources().getString(R.string.status_todo),
                getActivity().getResources().getString(R.string.status_in_progress),
                getActivity().getResources().getString(R.string.status_complete)
        };
    }
}